<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use app\common\controller\Curls;

class Valiates extends Controller
{
	private $token = 'work';
	private $access_token;
	private $appId = 'wxfe8a528cd8fc968d';
	private $appSecret = '6003847226b40fa38f032d143d64ce35';
	private function getToken()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
		$res = json_decode(Curls::curl($url),true);
		return $res['access_token'];
	}
	public function __construct()
	{
		$this->access_token = $this->getToken();
	}
	public function valid(Request $request)
	{
		$echoStr = $request->param('echostr');
		$signature = $request->param('signature');
		$timestamp = $request->param('timestamp');
		$nonce = $request->param('nonce');
		if($this->checkSignature($signature,$timestamp,$nonce)){
			echo $echoStr;
			exit;
		}
	}
	public function responseMsg()
	{
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$fromUsername = $postObj->FromUserName;
		$toUsername = $postObj->ToUserName;
		$keyword = trim($postObj->Content);
		$time = time();
		$textTpl = "<xml>
       <ToUserName><![CDATA[%s]]></ToUserName>
       <FromUserName><![CDATA[%s]]></FromUserName>
       <CreateTime>%s</CreateTime>
       <MsgType><![CDATA[%s]]></MsgType>
       <Content><![CDATA[%s]]></Content>
       <FuncFlag>0</FuncFlag>
       </xml>";
		if($keyword == "?" || $keyword == "?"){
			$msgType = "text";
			$contentStr = date("Y-m-d H:i:s",time());
			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
			echo $resultStr;
		}else {
			echo "";
			exit;
		}
	}
	public function checkSignature($signature,$timestamp,$nonce)
	{
		$token = $this->token;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 设置菜单
	 */
	public function menu()
	{
		$button = [
				"button" => [
						[
								"type" => "view",
								"name" => "消息通知",
								"url" => "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfe8a528cd8fc968d&url_param=1&redirect_uri=https%3A%2F%2Foa.gsmjb.com%2Findex%2Fwxpublic%2FwxLogin.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"
						],
						[
								"type" => "view",
								"name" => "打卡考勤",
								"url" => "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfe8a528cd8fc968d&url_param=2&redirect_uri=https%3A%2F%2Foa.gsmjb.com%2Findex%2Fwxpublic%2FwxLogin.html&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect"
						],
						[
								"type" => "view",
								"name" => "个人中心",
								"url" => "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfe8a528cd8fc968d&url_param=3&redirect_uri=https%3A%2F%2Foa.gsmjb.com%2Findex%2Fwxpublic%2FwxLogin.html&response_type=code&scope=snsapi_userinfo&state=3#wechat_redirect"
						]
				]
		];
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$this->access_token}";
		$res = Curls::http_post_json($url,json_encode($button,JSON_UNESCAPED_UNICODE));
		dump($res);
	}
	
}